﻿///*****************************************************
// * 本类库的核心系 JNTemplate
// * (C) JNTemplate 2011
// * 作者：翅膀的初衷 QQ:4585839
// * 网址：http://www.JiNianNet.com
// *****************************************************/
//using System;
//using System.Collections.Generic;
//using System.Text;
////using Microsoft.JScript;
//using System.Reflection;
//using Microsoft.CSharp;
//using System.CodeDom.Compiler;

//namespace JinianNet.JNTemplate
//{
//    public class Evaluator
//    {
//        #region CS
//        public static object Eval(string expr)
//        {
//            CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();

//            CompilerParameters objCompilerParameters = new CompilerParameters();
//            objCompilerParameters.ReferencedAssemblies.Add("System.dll");
//            objCompilerParameters.GenerateExecutable = false;
//            objCompilerParameters.GenerateInMemory = true;

//            CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromSource(objCompilerParameters, @"using System;
//namespace JinianNet.JNTemplate
//{
//    public class TempRunAction
//    {
//        public object Eval()
//        {
//            " + expr + @"
//        }
//    }
//}
//");
//            if (cr.Errors.HasErrors)
//            {
//                StringBuilder sbr = new StringBuilder("解析错误：");
//                //Console.WriteLine("编译错误：");
//                foreach (CompilerError err in cr.Errors)
//                {
//                    sbr.Append(err.ErrorText);
//                }
//                return sbr.ToString();
//            }
//            else
//            {
//                // 通过反射，调用HelloWorld的实例
//                Assembly objAssembly = cr.CompiledAssembly;
//                object objRun = objAssembly.CreateInstance("JNTemplate.TempRunAction");
//                MethodInfo objMI = objRun.GetType().GetMethod("Eval");

//                return objMI.Invoke(objRun, null);
//            }


//        }
//        #endregion
//        #region JS
//        //        private static Type _evaluatorType = null;
////        private static object _evaluator = null;
////        private static readonly string _jscriptSource =
////            @"package Evaluator
////            {
////               class Evaluator
////               {
////                  public function Eval(expr : String) : String 
////                  { 
////                     return eval(expr); 
////                  }
////               }
////            }";

////        static Evaluator()
////        {
////            ICodeCompiler compiler = new JScriptCodeProvider().CreateCompiler();
////            CompilerParameters parameters = new CompilerParameters();
////            parameters.GenerateInMemory = true;
////            CompilerResults results= compiler.CompileAssemblyFromSource(parameters, _jscriptSource);
////            Assembly assembly = results.CompiledAssembly;
////            _evaluatorType = assembly.GetType("Evaluator.Evaluator");
////            _evaluator = Activator.CreateInstance(_evaluatorType);

////        }
////        public static object Eval(string expr)
////        {
////            return _evaluatorType.InvokeMember(
////                        "Eval",
////                        BindingFlags.InvokeMethod,
////                        null,
////                        _evaluator,
////                        new object[] { expr }
////                     );

//        //        }
//        #endregion

//    }
//}
